// OUTDOOR SECTION SCRIPT
//    Section: X = 0, Y = 1

// This is the special encounter script for this town.
// The states INIT_STATE and START_STATE have
// meanings that are described in the documentation. States you write
// yourself should be numbered from 10-100.

//
// flags:
// 201, 0 = Bridge message
// 201, 1 = farmhouse by bridge (ne)
// 201, 2 = farmhouse by bridge (se)
// 201, 3 = southern farmhouse
// 201, 4 = crevasse message flag
// 201, 5 =  " " "
// 201, 6 = after winning, rmv enc flag check
//

beginoutdoorscript;

variables;

short choice, i, j, k, l;
string msg;

body;

beginstate INIT_STATE;
// This state called whenever this section is loaded into memory.
break;

beginstate START_STATE;
	// one time check after winning to remove outdoor encounters that may exist
	if ((get_flag(50, 1) > 0) && (get_flag(201, 6) == FALSE)) {
		set_flag(201, 6, TRUE);
		if (outdoor_enc_exists(50, 1) == TRUE) {
			eliminate_outdoor_enc(50, 1);
		}
	}
break;

beginstate 10;
	if (get_flag(201, 0) == FALSE) {
		set_flag(201, 0, TRUE);
		message_dialog("The bridge ahead looks like it might fall at any moment.",
			"Are you sure you want to cross?");
	}
break;

beginstate 11;
	// check if slip on bridge
	clear_buffer();
	i = get_ran(1, 1, 100);
	if (i <= 50) {
		// slipped, lost items?
		i = get_ran(1, 1, 100);
		if (i <= 25) {
			// potentially lost something, pick a char
			k = 10; l = 0;
			j = party_size() - 1;
			while ((k > 5) && (l < 10)) {
				i = get_ran(1, 0, j);
				if (char_ok(i)) k = i;
				l = l + 1;
			}
			if (k == 10) { k = 0; } // do first char
			if (char_ok(k)) {
				append_char_name(k);
				// take something from them...
				i = get_ran(1, 13, 39);
				j = item_type_in_slot(k, i);
				if ((j > 0) && (j != 450)) {
					// item gone forever...
					destroy_char_item(k, i);
					append_string(" fell down.  An item being carried dropped into the water below, lost forever.");
				}
				else {
					append_string(" fell to one knee, but managed to get steadied before falling completely.");
				}
			}
			else {
				append_string("Luckily, no one fell.");
			}
		}
		else {
			append_string("You manage to catch yourself before falling.");
		}
		get_buffer_text(msg);
		message_dialog("Whoops!  You slip on a loose board.", msg);
	}
break;

beginstate 12;
	if (get_flag(201, 1) == FALSE) {
		set_flag(201, 1, TRUE);
		message_dialog("The farmhouse is empty.",
			"You don't see the farmer anywhere around, but then again, your quest doesn't include finding farmers.");
	}
break;

beginstate 13;
	if (get_flag(201, 2) == FALSE) {
		set_flag(201, 2, TRUE);
		message_dialog("The farmhouse is unoccupied at the moment.",
			"You thought you'd seen the farmer out in the field, but he doesn't appear to be there now.");
	}
break;

beginstate 14;
	if (get_flag(201, 3) == FALSE) {
		set_flag(201, 3, TRUE);
		message_dialog("No one seems to be home currently.",
			"Perhaps the farmer is in town, staying away from what ails The Wilderness.");
	}
break;

beginstate 15;
	if (get_flag(51, 1) > 0) {
		// you know there's a crevasse here...
		if (get_flag(201, 4) == FALSE) {
			set_flag(201, 4, TRUE);
			message_dialog("As you are walking past the cliff, the strange play of shadows on the cliff face catches your eye.",
					"It is a crevasse leading back into the cliff, just where the map in the book showed it to be.");
		}
	}
	else {
		if (get_flag(201, 5) == FALSE) {
			set_flag(201, 5, TRUE);
			message_dialog("You think you see something in the shadows on the cliff face.",
					"As you turn to look, you see nothing by cliff face staring back at you.");
		}
	}
break;

beginstate 16;
	if (get_flag(51, 2) == FALSE) {
		reset_dialog();
		add_dialog_str(0, "On the ground, by the cliff edge, sits a pile of bones that used to be a couple of Zanta followers.", 0);
		add_dialog_str(1, "There is not much left, rotting leather bracers, rusted metal, nothing recognizable.", 0);
		add_dialog_choice(0, "Search");
		add_dialog_choice(1, "Leave");
		choice = run_dialog(FALSE);
		if (choice == 1) {
			set_flag(51, 2, TRUE);
			message_dialog("As you sift through the bones, a shiny, wood-like item catches your eye.",
					"It's a piece of the staff!");
			change_spec_item(2, 1);
		}
	}
break;

beginstate 17;
	if (get_flag(51, 1) == FALSE) {
		block_entry(TRUE);
	}
	else {
		block_entry(FALSE);
	}
break;
